home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / comint / gdb.el.z / gdb.el
Encoding:
Text File  |  1998-05-21  |  23.9 KB  |  701 lines

  1. ;;; gdb.el --- run gdb under Emacs
  2.  
  3. ;; Author: W. Schelter, University of Texas
  4. ;;     wfs@rascal.ics.utexas.edu
  5. ;; Rewritten by rms.
  6. ;; Keywords: c, unix, tools, debugging
  7.  
  8. ;; Some ideas are due to Masanobu.
  9.  
  10. ;; This file is part of XEmacs.
  11.  
  12. ;; XEmacs is free software; you can redistribute it and/or modify it
  13. ;; under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16.  
  17. ;; XEmacs is distributed in the hope that it will be useful, but
  18. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  20. ;; General Public License for more details.
  21.  
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  24. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  25. ;; 02111-1307, USA.
  26.  
  27. ;;; Synched up with: Not in FSF
  28.  
  29. ;;; Commentary:
  30.  
  31. ;; Description of GDB interface:
  32.  
  33. ;; A facility is provided for the simultaneous display of the source code
  34. ;; in one window, while using gdb to step through a function in the
  35. ;; other.  A small arrow in the source window, indicates the current
  36. ;; line.
  37.  
  38. ;; Starting up:
  39.  
  40. ;; In order to use this facility, invoke the command GDB to obtain a
  41. ;; shell window with the appropriate command bindings.  You will be asked
  42. ;; for the name of a file to run.  Gdb will be invoked on this file, in a
  43. ;; window named *gdb-foo* if the file is foo.
  44.  
  45. ;; M-s steps by one line, and redisplays the source file and line.
  46.  
  47. ;; You may easily create additional commands and bindings to interact
  48. ;; with the display.  For example to put the gdb command next on \M-n
  49. ;; (def-gdb next "\M-n")
  50.  
  51. ;; This causes the emacs command gdb-next to be defined, and runs
  52. ;; gdb-display-frame after the command.
  53.  
  54. ;; gdb-display-frame is the basic display function.  It tries to display
  55. ;; in the other window, the file and line corresponding to the current
  56. ;; position in the gdb window.  For example after a gdb-step, it would
  57. ;; display the line corresponding to the position for the last step.  Or
  58. ;; if you have done a backtrace in the gdb buffer, and move the cursor
  59. ;; into one of the frames, it would display the position corresponding to
  60. ;; that frame.
  61.  
  62. ;; gdb-display-frame is invoked automatically when a filename-and-line-number
  63. ;; appears in the output.
  64.  
  65. ;;; Code:
  66.  
  67. (require 'comint)
  68. (require 'shell)
  69.  
  70. (condition-case nil
  71.     (if (featurep 'toolbar)
  72.     (require 'eos-toolbar "sun-eos-toolbar"))
  73.   (error nil))
  74.  
  75. (defvar gdb-last-frame)
  76. (defvar gdb-delete-prompt-marker)
  77. (defvar gdb-filter-accumulator)
  78. (defvar gdb-last-frame-displayed-p)
  79. (defvar gdb-arrow-extent nil)
  80. (or (fboundp 'make-glyph) (fset 'make-glyph 'identity)) ; work w/ pre beta v12
  81. (defvar gdb-arrow-glyph (make-glyph "=>"))
  82.  
  83. (make-face 'gdb-arrow-face)
  84. (or (face-differs-from-default-p 'gdb-arrow-face)
  85.    ;; Usually has a better default value than highlight does
  86.    (copy-face 'isearch 'gdb-arrow-face))
  87.  
  88. ;; Hooks can side-effect extent arg to change extent properties
  89. (defvar gdb-arrow-extent-hooks '())
  90.  
  91. (defvar gdb-prompt-pattern "^>\\|^(.*gdb[+]?) *\\|^---Type <return> to.*--- *"
  92.   "A regexp to recognize the prompt for gdb or gdb+.") 
  93.  
  94. (defvar gdb-mode-map nil
  95.   "Keymap for gdb-mode.")
  96.  
  97. (defvar gdb-toolbar
  98.   '([eos::toolbar-stop-at-icon
  99.      gdb-toolbar-break
  100.      t
  101.      "Stop at selected position"]
  102.     [eos::toolbar-stop-in-icon
  103.      gdb-toolbar-break
  104.      t
  105.      "Stop in function whose name is selected"]
  106.     [eos::toolbar-clear-at-icon
  107.      gdb-toolbar-clear
  108.      t
  109.      "Clear at selected position"]
  110.     [eos::toolbar-evaluate-icon
  111.      nil
  112.      nil
  113.      "Evaluate selected expression; shows in separate XEmacs frame"]
  114.     [eos::toolbar-evaluate-star-icon
  115.      nil
  116.      nil
  117.      "Evaluate selected expression as a pointer; shows in separate XEmacs frame"]
  118.     [eos::toolbar-run-icon
  119.      gdb-run
  120.      t
  121.      "Run current program"]
  122.     [eos::toolbar-cont-icon
  123.      gdb-cont
  124.      t
  125.      "Continue current program"]
  126.     [eos::toolbar-step-into-icon
  127.      gdb-step
  128.      t
  129.      "Step into (aka step)"]
  130.     [eos::toolbar-step-over-icon
  131.      gdb-next
  132.      t
  133.      "Step over (aka next)"]
  134.     [eos::toolbar-up-icon
  135.      gdb-up
  136.      t
  137.      "Stack Up (towards \"cooler\" - less recently visited - frames)"]
  138.     [eos::toolbar-down-icon
  139.      gdb-down
  140.      t
  141.      "Stack Down (towards \"warmer\" - more recently visited - frames)"]
  142.     [eos::toolbar-fix-icon    nil    nil    "Fix (not available with gdb)"]
  143.     [eos::toolbar-build-icon
  144.      toolbar-compile
  145.      t
  146.      "Build (aka make -NYI)"]
  147.     ))
  148.  
  149. (if gdb-mode-map
  150.    nil
  151.   (setq gdb-mode-map (make-sparse-keymap))
  152.   (set-keymap-name gdb-mode-map 'gdb-mode-map)
  153.   (set-keymap-parents gdb-mode-map (list comint-mode-map))
  154.   (define-key gdb-mode-map "\C-l" 'gdb-refresh)
  155.   (define-key gdb-mode-map "\C-c\C-c" 'gdb-control-c-subjob)
  156.   (define-key gdb-mode-map "\t" 'comint-dynamic-complete)
  157.   (define-key gdb-mode-map "\M-?" 'comint-dynamic-list-completions))
  158.  
  159. (define-key ctl-x-map " " 'gdb-break)
  160. (define-key ctl-x-map "&" 'send-gdb-command)
  161.  
  162. ;;Of course you may use `def-gdb' with any other gdb command, including
  163. ;;user defined ones.   
  164.  
  165. (defmacro def-gdb (name key &optional doc &rest forms)
  166.   (let* ((fun (intern (format "gdb-%s" name)))
  167.      (cstr (list 'if '(not (= 1 arg))
  168.              (list 'format "%s %s" name 'arg)
  169.              name)))
  170.     (list 'progn
  171.       (nconc (list 'defun fun '(arg)
  172.                (or doc "")
  173.                '(interactive "p")
  174.                (list 'gdb-call cstr))
  175.          forms)
  176.       (and key (list 'define-key 'gdb-mode-map key  (list 'quote fun))))))
  177.  
  178. (def-gdb "step"   "\M-s" "Step one source line with display"
  179.   (gdb-delete-arrow-extent))
  180. (def-gdb "stepi"  "\M-i" "Step one instruction with display"
  181.   (gdb-delete-arrow-extent))
  182. (def-gdb "finish" "\C-c\C-f" "Finish executing current function"
  183.   (gdb-delete-arrow-extent))
  184. (def-gdb "run" nil "Run the current program"
  185.   (gdb-delete-arrow-extent))
  186.  
  187. ;;"next" and "cont" were bound to M-n and M-c in Emacs 18, but these are
  188. ;;poor choices, since M-n is used for history navigation and M-c is
  189. ;;capitalize-word.  These are defined without key bindings so that users
  190. ;;may choose their own bindings.
  191. (def-gdb "next"   "\C-c\C-n" "Step one source line (skip functions)"
  192.   (gdb-delete-arrow-extent))
  193. (def-gdb "cont"   "\C-c\M-c" "Proceed with the program"
  194.   (gdb-delete-arrow-extent))
  195.  
  196. (def-gdb "up"     "\C-c<" "Go up N stack frames (numeric arg) with display")
  197. (def-gdb "down"   "\C-c>" "Go down N stack frames (numeric arg) with display")
  198.  
  199. (defvar gdb-display-mode nil
  200.   "Minor mode for gdb frame display")
  201. (or (assq 'gdb-display-mode minor-mode-alist)
  202.     (setq minor-mode-alist
  203.       (purecopy
  204.        (append minor-mode-alist
  205.            '((gdb-display-mode " Frame"))))))
  206.  
  207. (defun gdb-display-mode (&optional arg)
  208.   "Toggle GDB Frame display mode
  209. With arg, turn display mode on if and only if arg is positive.
  210. In the display minor mode, source file are displayed in another
  211. window for repective \\[gdb-display-frame] commands."
  212.   (interactive "P")
  213.   (setq gdb-display-mode (if (null arg)
  214.                  (not gdb-display-mode)
  215.                (> (prefix-numeric-value arg) 0))))
  216.  
  217. ;; Using cc-mode's syntax table is broken.
  218. (defvar gdb-mode-syntax-table nil
  219.   "Syntax table for GDB mode.")
  220.  
  221. ;; This is adapted from CC Mode 5.11.
  222. (unless gdb-mode-syntax-table
  223.   (setq gdb-mode-syntax-table (make-syntax-table))
  224.   ;; DO NOT TRY TO SET _ (UNDERSCORE) TO WORD CLASS!
  225.   (modify-syntax-entry ?_  "_" gdb-mode-syntax-table)
  226.   (modify-syntax-entry ?\\ "\\" gdb-mode-syntax-table)
  227.   (modify-syntax-entry ?+  "." gdb-mode-syntax-table)
  228.   (modify-syntax-entry ?-  "." gdb-mode-syntax-table)
  229.   (modify-syntax-entry ?=  "." gdb-mode-syntax-table)
  230.   (modify-syntax-entry ?%  "." gdb-mode-syntax-table)
  231.   (modify-syntax-entry ?<  "." gdb-mode-syntax-table)
  232.   (modify-syntax-entry ?>  "." gdb-mode-syntax-table)
  233.   (modify-syntax-entry ?&  "." gdb-mode-syntax-table)
  234.   (modify-syntax-entry ?|  "." gdb-mode-syntax-table)
  235.   (modify-syntax-entry ?\' "\"" gdb-mode-syntax-table)
  236.   ;; add extra comment syntax
  237.   (modify-syntax-entry ?/  ". 14"  gdb-mode-syntax-table)
  238.   (modify-syntax-entry ?*  ". 23"  gdb-mode-syntax-table))
  239.  
  240.  
  241. (defun gdb-mode ()
  242.   "Major mode for interacting with an inferior Gdb process.
  243. The following commands are available:
  244.  
  245. \\{gdb-mode-map}
  246.  
  247. \\[gdb-display-frame] displays in the other window
  248. the last line referred to in the gdb buffer. See also
  249. \\[gdb-display-mode].
  250.  
  251. \\[gdb-step],\\[gdb-next], and \\[gdb-nexti] in the gdb window,
  252. call gdb to step,next or nexti and then update the other window
  253. with the current file and position.
  254.  
  255. If you are in a source file, you may select a point to break
  256. at, by doing \\[gdb-break].
  257.  
  258. Commands:
  259. Many commands are inherited from comint mode. 
  260. Additionally we have:
  261.  
  262. \\[gdb-display-frame] display frames file in other window
  263. \\[gdb-step] advance one line in program
  264. \\[send-gdb-command] used for special printing of an arg at the current point.
  265. C-x SPACE sets break point at current line."
  266.   (interactive)
  267.   (comint-mode)
  268.   (use-local-map gdb-mode-map)
  269.   (set-syntax-table gdb-mode-syntax-table)
  270.   (make-local-variable 'gdb-last-frame-displayed-p)
  271.   (make-local-variable 'gdb-last-frame)
  272.   (make-local-variable 'gdb-delete-prompt-marker)
  273.   (make-local-variable 'gdb-display-mode)
  274.   (make-local-variable' gdb-filter-accumulator)
  275.   (setq gdb-last-frame nil
  276.         gdb-delete-prompt-marker nil
  277.         gdb-filter-accumulator nil
  278.     gdb-display-mode t
  279.         major-mode 'gdb-mode
  280.         mode-name "Inferior GDB"
  281.         comint-prompt-regexp gdb-prompt-pattern
  282.         gdb-last-frame-displayed-p t)
  283.   (set (make-local-variable 'shell-dirtrackp) t)
  284.   ;;(make-local-variable 'gdb-arrow-extent)
  285.   (and (extentp gdb-arrow-extent)
  286.        (delete-extent gdb-arrow-extent))
  287.   (setq gdb-arrow-extent nil)
  288.   ;; XEmacs change:
  289.   (make-local-hook 'kill-buffer-hook)
  290.   (add-hook 'kill-buffer-hook 'gdb-delete-arrow-extent nil t)
  291.   (add-hook 'comint-input-filter-functions 'shell-directory-tracker nil t)
  292.   (run-hooks 'gdb-mode-hook))
  293.  
  294. (defun gdb-delete-arrow-extent ()
  295.   (let ((inhibit-quit t))
  296.     (if gdb-arrow-extent
  297.         (delete-extent gdb-arrow-extent))
  298.     (setq gdb-arrow-extent nil)))
  299.  
  300. (defvar current-gdb-buffer nil)
  301.  
  302. ;;;###autoload
  303. (defvar gdb-command-name "gdb"
  304.   "Pathname for executing gdb.")
  305.  
  306. ;;;###autoload
  307. (defun gdb (path &optional corefile)
  308.   "Run gdb on program FILE in buffer *gdb-FILE*.
  309. The directory containing FILE becomes the initial working directory
  310. and source-file directory for GDB.  If you wish to change this, use
  311. the GDB commands `cd DIR' and `directory'."
  312.   (interactive "FRun gdb on file: ")
  313.   (setq path (file-truename (expand-file-name path)))
  314.   (let ((file (file-name-nondirectory path)))
  315.     (switch-to-buffer (concat "*gdb-" file "*"))
  316.     (setq default-directory (file-name-directory path))
  317.     (or (bolp) (newline))
  318.     (insert "Current directory is " default-directory "\n")
  319.     (apply 'make-comint
  320.        (concat "gdb-" file)
  321.        (substitute-in-file-name gdb-command-name)
  322.        nil
  323.        "-fullname"
  324.        "-cd" default-directory
  325.        file
  326.        (and corefile (list corefile)))
  327.     (set-process-filter (get-buffer-process (current-buffer)) 'gdb-filter)
  328.     (set-process-sentinel (get-buffer-process (current-buffer)) 'gdb-sentinel)
  329.     ;; XEmacs change: turn on gdb mode after setting up the proc filters
  330.     ;; for the benefit of shell-font.el
  331.     (gdb-mode)
  332.     (gdb-set-buffer)))
  333.  
  334. ;;;###autoload
  335. (defun gdb-with-core (file corefile)
  336.   "Debug a program using a corefile."
  337.   (interactive "fProgram to debug: \nfCore file to use: ")
  338.   (gdb file corefile))
  339.  
  340. (defun gdb-set-buffer ()
  341.   (cond ((eq major-mode 'gdb-mode)
  342.      (setq current-gdb-buffer (current-buffer))
  343.      (if (featurep 'eos-toolbar)
  344.          (set-specifier default-toolbar (cons (current-buffer)
  345.                           gdb-toolbar))))))
  346.  
  347.  
  348. ;; This function is responsible for inserting output from GDB
  349. ;; into the buffer.
  350. ;; Aside from inserting the text, it notices and deletes
  351. ;; each filename-and-line-number;
  352. ;; that GDB prints to identify the selected frame.
  353. ;; It records the filename and line number, and maybe displays that file.
  354. (defun gdb-filter (proc string)
  355.   (let ((inhibit-quit t))
  356.     (save-current-buffer
  357.      (set-buffer (process-buffer proc))
  358.      (if gdb-filter-accumulator
  359.      (gdb-filter-accumulate-marker
  360.       proc (concat gdb-filter-accumulator string))
  361.        (gdb-filter-scan-input proc string)))))
  362.  
  363. (defun gdb-filter-accumulate-marker (proc string)
  364.   (setq gdb-filter-accumulator nil)
  365.   (if (> (length string) 1)
  366.       (if (= (aref string 1) ?\032)
  367.       (let ((end (string-match "\n" string)))
  368.         (if end
  369.         (progn
  370.           (let* ((first-colon (string-match ":" string 2))
  371.              (second-colon
  372.               (string-match ":" string (1+ first-colon))))
  373.             (setq gdb-last-frame
  374.               (cons (substring string 2 first-colon)
  375.                 (string-to-int
  376.                  (substring string (1+ first-colon)
  377.                         second-colon)))))
  378.           (setq gdb-last-frame-displayed-p nil)
  379.           (gdb-filter-scan-input proc
  380.                      (substring string (1+ end))))
  381.           (setq gdb-filter-accumulator string)))
  382.     (gdb-filter-insert proc "\032")
  383.     (gdb-filter-scan-input proc (substring string 1)))
  384.     (setq gdb-filter-accumulator string)))
  385.  
  386. (defun gdb-filter-scan-input (proc string)
  387.   (if (equal string "")
  388.       (setq gdb-filter-accumulator nil)
  389.     (let ((start (string-match "\032" string)))
  390.       (if start
  391.       (progn (gdb-filter-insert proc (substring string 0 start))
  392.          (gdb-filter-accumulate-marker proc
  393.                            (substring string start)))
  394.     (gdb-filter-insert proc string)))))
  395.  
  396. (defun gdb-filter-insert (proc string)
  397.   (let ((moving (= (point) (process-mark proc)))
  398.     (output-after-point (< (point) (process-mark proc))))
  399.     (save-excursion
  400.       ;; Insert the text, moving the process-marker.
  401.       (goto-char (process-mark proc))
  402.       (insert-before-markers string)
  403.       (set-marker (process-mark proc) (point))
  404.       (gdb-maybe-delete-prompt)
  405.       ;; Check for a filename-and-line number.
  406.       (gdb-display-frame
  407.        ;; Don't display the specified file
  408.        ;; unless (1) point is at or after the position where output appears
  409.        ;; and (2) this buffer is on the screen.
  410.        (or output-after-point
  411.            (not (get-buffer-window (current-buffer))))
  412.        ;; Display a file only when a new filename-and-line-number appears.
  413.        t))
  414.     (if moving (goto-char (process-mark proc))))
  415.  
  416.   (let (s)
  417.     (if (and (should-use-dialog-box-p)
  418.          (setq s (or (string-match " (y or n) *\\'" string)
  419.              (string-match " (yes or no) *\\'" string))))
  420.     (gdb-mouse-prompt-hack (substring string 0 s) (current-buffer))))
  421.   )
  422.  
  423. (defun gdb-mouse-prompt-hack (prompt buffer)
  424.   (popup-dialog-box
  425.    (list prompt
  426.      (vector "Yes"    (list 'gdb-mouse-prompt-hack-answer 't   buffer) t)
  427.      (vector "No"     (list 'gdb-mouse-prompt-hack-answer 'nil buffer) t)
  428.      nil
  429.      (vector "Cancel" (list 'gdb-mouse-prompt-hack-answer 'nil buffer) t)
  430.      )))
  431.  
  432. (defun gdb-mouse-prompt-hack-answer (answer buffer)
  433.   (let ((b (current-buffer)))
  434.     (unwind-protect
  435.     (progn
  436.       (set-buffer buffer)
  437.       (goto-char (process-mark (get-buffer-process buffer)))
  438.       (delete-region (point) (point-max))
  439.       (insert (if answer "yes" "no"))
  440.       (comint-send-input))
  441.       (set-buffer b))))
  442.  
  443. (defun gdb-sentinel (proc msg)
  444.   (cond ((null (buffer-name (process-buffer proc)))
  445.      ;; buffer killed
  446.      ;; Stop displaying an arrow in a source file.
  447.      ;(setq overlay-arrow-position nil) -- done by kill-buffer-hook
  448.      (set-process-buffer proc nil))
  449.     ((memq (process-status proc) '(signal exit))
  450.      ;; Stop displaying an arrow in a source file.
  451.          (gdb-delete-arrow-extent)
  452.      ;; Fix the mode line.
  453.      (setq modeline-process
  454.            (concat ": gdb " (symbol-name (process-status proc))))
  455.      (let* ((obuf (current-buffer)))
  456.        ;; save-excursion isn't the right thing if
  457.        ;;  process-buffer is current-buffer
  458.        (unwind-protect
  459.            (progn
  460.          ;; Write something in *compilation* and hack its mode line,
  461.          (set-buffer (process-buffer proc))
  462.          ;; Force mode line redisplay soon
  463.          (set-buffer-modified-p (buffer-modified-p))
  464.          (if (eobp)
  465.              (insert ?\n mode-name " " msg)
  466.            (save-excursion
  467.              (goto-char (point-max))
  468.              (insert ?\n mode-name " " msg)))
  469.          ;; If buffer and mode line will show that the process
  470.          ;; is dead, we can delete it now.  Otherwise it
  471.          ;; will stay around until M-x list-processes.
  472.          (delete-process proc))
  473.          ;; Restore old buffer, but don't restore old point
  474.          ;; if obuf is the gdb buffer.
  475.          (set-buffer obuf))))))
  476.  
  477.  
  478. (defun gdb-refresh (&optional arg)
  479.   "Fix up a possibly garbled display, and redraw the arrow."
  480.   (interactive "P")
  481.   (recenter arg)
  482.   (gdb-display-frame))
  483.  
  484. (defun gdb-display-frame (&optional nodisplay noauto)
  485.   "Find, obey and delete the last filename-and-line marker from GDB.
  486. The marker looks like \\032\\032FILENAME:LINE:CHARPOS\\n.
  487. Obeying it means displaying in another window the specified file and line."
  488.   (interactive)
  489.   (gdb-set-buffer)
  490.   (and gdb-last-frame (not nodisplay)
  491.        gdb-display-mode
  492.        (or (not gdb-last-frame-displayed-p) (not noauto))
  493.        (progn (gdb-display-line (car gdb-last-frame) (cdr gdb-last-frame))
  494.           (setq gdb-last-frame-displayed-p t))))
  495.  
  496. ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
  497. ;; and that its line LINE is visible.
  498. ;; Put the overlay-arrow on the line LINE in that buffer.
  499.  
  500. (defun gdb-display-line (true-file line &optional select-method)
  501.   ;; FILE to display
  502.   ;; LINE number to highlight and make visible
  503.   ;; SELECT-METHOD 'source, 'debugger, or 'none.  (default is 'debugger)
  504.   (and (null select-method) (setq select-method 'debugger))
  505.   (let* ((pre-display-buffer-function nil) ; screw it, put it all in one screen
  506.      (pop-up-windows t)
  507.      (source-buffer (find-file-noselect true-file))
  508.      (source-window (display-buffer source-buffer))
  509.      (debugger-window (get-buffer-window current-gdb-buffer))
  510.          (extent gdb-arrow-extent)
  511.      pos)
  512.     ;; XEmacs change: make sure we find a window displaying the source file
  513.     ;; even if we are already sitting in it when a breakpoint is hit.
  514.     ;; Otherwise the t argument to display-buffer will prevent it from being
  515.     ;; displayed.
  516.     (save-excursion 
  517.       (cond ((eq select-method 'debugger)
  518.          ;; might not already be displayed
  519.          (setq debugger-window (display-buffer current-gdb-buffer))
  520.          (select-window debugger-window))
  521.         ((eq select-method 'source)
  522.          (select-window source-window))))
  523.     (and extent
  524.      (not (eq (extent-object extent) source-buffer))
  525.      (setq extent (delete-extent extent)))
  526.     (or extent
  527.         (progn
  528.           (setq extent (make-extent 1 1 source-buffer))
  529.           (set-extent-face extent 'gdb-arrow-face)
  530.       (set-extent-begin-glyph extent gdb-arrow-glyph)
  531.           (set-extent-begin-glyph-layout extent 'whitespace)
  532.           (set-extent-priority extent 2000)
  533.           (setq gdb-arrow-extent extent)))
  534.     (save-current-buffer
  535.       (set-buffer source-buffer)
  536.       (save-restriction
  537.     (widen)
  538.     (goto-line line)
  539.     (set-window-point source-window (point))
  540.     (setq pos (point))
  541.         (end-of-line)
  542.         (set-extent-endpoints extent pos (point))
  543.         (run-hook-with-args 'gdb-arrow-extent-hooks extent))
  544.       (cond ((or (< pos (point-min)) (> pos (point-max)))
  545.          (widen)
  546.          (goto-char pos))))
  547.     ;; Added by Stig.  It caused lots of problems for several users
  548.     ;; and since its purpose is unclear it is getting commented out.
  549.     ;;(and debugger-window
  550.     ;; (set-window-point debugger-window pos))
  551.     ))
  552.  
  553. (defun gdb-call (command)
  554.   "Invoke gdb COMMAND displaying source in other window."
  555.   (interactive)
  556.   (goto-char (point-max))
  557.   ;; Record info on the last prompt in the buffer and its position.
  558.   ;; This is used in  gdb-maybe-delete-prompt
  559.   ;; to prevent multiple prompts from accumulating.
  560.   (save-excursion
  561.     (goto-char (process-mark (get-buffer-process current-gdb-buffer)))
  562.     (let ((pt (point)))
  563.       (beginning-of-line)
  564.       (setq gdb-delete-prompt-marker
  565.         (if (= (point) pt)
  566.         nil
  567.           (list (point-marker) (- pt (point))
  568.             (buffer-substring (point) pt))))))
  569.   (gdb-set-buffer)
  570.   (process-send-string (get-buffer-process current-gdb-buffer)
  571.            (concat command "\n")))
  572.  
  573. (defun gdb-maybe-delete-prompt ()
  574.   (if gdb-delete-prompt-marker
  575.       ;; Get the string that we used as the prompt before.
  576.       (let ((prompt (nth 2 gdb-delete-prompt-marker))
  577.         (length (nth 1 gdb-delete-prompt-marker)))
  578.     ;; Position after it.
  579.     (goto-char (+ (car gdb-delete-prompt-marker) length))
  580.     ;; Delete any duplicates of it which follow right after.
  581.     (while (and (<= (+ (point) length) (point-max))
  582.             (string= prompt
  583.                  (buffer-substring (point) (+ (point) length))))
  584.       (delete-region (point) (+ (point) length)))
  585.     ;; If that didn't take us to where output is arriving,
  586.     ;; we have encountered something other than a prompt,
  587.     ;; so stop trying to delete any more prompts.
  588.     (if (not (= (point)
  589.             (process-mark (get-buffer-process current-gdb-buffer))))
  590.         (progn
  591.           (set-marker (car gdb-delete-prompt-marker) nil)
  592.           (setq gdb-delete-prompt-marker nil))))))
  593.  
  594. (defun gdb-break (temp)
  595.   "Set GDB breakpoint at this source line.  With ARG set temporary breakpoint."
  596.   (interactive "P")
  597.   (let* ((file-name (file-name-nondirectory buffer-file-name))
  598.      (line (save-restriction
  599.          (widen)
  600.          (beginning-of-line)
  601.          (1+ (count-lines 1 (point)))))
  602.      (cmd (concat (if temp "tbreak " "break ") file-name ":"
  603.               (int-to-string line))))
  604.     (set-buffer current-gdb-buffer)
  605.     (goto-char (process-mark (get-buffer-process current-gdb-buffer)))
  606.     (delete-region (point) (point-max))
  607.     (insert cmd)
  608.     (comint-send-input)
  609.     ;;(process-send-string (get-buffer-process current-gdb-buffer) cmd)
  610.     ))
  611.  
  612. (defun gdb-clear ()
  613.   "Set GDB breakpoint at this source line."
  614.   (interactive)
  615.   (let* ((file-name (file-name-nondirectory buffer-file-name))
  616.      (line (save-restriction
  617.          (widen)
  618.          (beginning-of-line)
  619.          (1+ (count-lines 1 (point)))))
  620.      (cmd (concat "clear " file-name ":"
  621.               (int-to-string line))))
  622.     (set-buffer current-gdb-buffer)
  623.     (goto-char (process-mark (get-buffer-process current-gdb-buffer)))
  624.     (delete-region (point) (point-max))
  625.     (insert cmd)
  626.     (comint-send-input)
  627.     ;;(process-send-string (get-buffer-process current-gdb-buffer) cmd)
  628.     ))
  629.  
  630. (defun gdb-read-address()
  631.   "Return a string containing the core-address found in the buffer at point."
  632.   (save-excursion
  633.    (let ((pt (point)) found begin)
  634.      (setq found (if (search-backward "0x" (- pt 7) t)(point)))
  635.      (cond (found (forward-char 2)
  636.           (buffer-substring found
  637.                     (progn (re-search-forward "[^0-9a-f]")
  638.                        (forward-char -1)
  639.                        (point))))
  640.        (t (setq begin (progn (re-search-backward "[^0-9]") (forward-char 1)
  641.                  (point)))
  642.           (forward-char 1)
  643.           (re-search-forward "[^0-9]")
  644.           (forward-char -1)
  645.           (buffer-substring begin (point)))))))
  646.  
  647.  
  648. (defvar gdb-commands nil
  649.   "List of strings or functions used by send-gdb-command.
  650. It is for customization by you.")
  651.  
  652. (defun send-gdb-command (arg)
  653.  
  654.   "This command reads the number where the cursor is positioned.  It
  655.  then inserts this ADDR at the end of the gdb buffer.  A numeric arg
  656.  selects the ARG'th member COMMAND of the list gdb-print-command.  If
  657.  COMMAND is a string, (format COMMAND ADDR) is inserted, otherwise
  658.  (funcall COMMAND ADDR) is inserted.  eg. \"p (rtx)%s->fld[0].rtint\"
  659.  is a possible string to be a member of gdb-commands.  "
  660.  
  661.  
  662.   (interactive "P")
  663.   (let (comm addr)
  664.     (if arg (setq comm (nth arg gdb-commands)))
  665.     (setq addr (gdb-read-address))
  666.     (if (eq (current-buffer) current-gdb-buffer)
  667.     (set-mark (point)))
  668.     (cond (comm
  669.        (setq comm
  670.          (if (stringp comm) (format comm addr) (funcall comm addr))))
  671.       (t (setq comm addr)))
  672.     (switch-to-buffer current-gdb-buffer)
  673.     (goto-char (point-max))
  674.     (insert comm)))
  675.  
  676. (fset 'gdb-control-c-subjob 'comint-interrupt-subjob)
  677.  
  678. ;(defun gdb-control-c-subjob ()
  679. ;  "Send a Control-C to the subprocess."
  680. ;  (interactive)
  681. ;  (process-send-string (get-buffer-process (current-buffer))
  682. ;               "\C-c"))
  683.  
  684. (defun gdb-toolbar-break ()
  685.   (interactive)
  686.   (save-excursion
  687.     (message (car gdb-last-frame))
  688.     (set-buffer (find-file-noselect (car gdb-last-frame)))
  689.     (gdb-break nil)))
  690.  
  691. (defun gdb-toolbar-clear ()
  692.   (interactive)
  693.   (save-excursion
  694.     (message (car gdb-last-frame))
  695.     (set-buffer (find-file-noselect (car gdb-last-frame)))
  696.     (gdb-clear)))
  697.  
  698. (provide 'gdb)
  699.  
  700. ;;; gdb.el ends here
  701.